package ui.model;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public 	class TableListModel extends AbstractTableModel {

	
	
	private Vector<String> columnNames = null;
	private Vector<Vector<String>> rows = null;
	private int columnCount = 0;
	private int rowCount = 0;
	// Methods
	public TableListModel(Vector<Vector<String>> rows, Vector<String> columnnames){
		this.columnNames = columnnames;
//		for (int i=0; i < columnNames.size(); i++){
//			System.out.println(columnNames.get(i));
//		}
		if (rows == null){
			rows = new Vector<Vector<String>>();
			Vector<String>tmp = new Vector<String>();
			for (String blub : columnnames){
				tmp.add("");
			}
			System.out.println("rows sind null");
		}	
		this.rows = rows;
		if (columnnames == null){
			System.out.println("columnnames sind null");
		}
		columnCount = columnNames.size();
		rowCount = rows.size();
		
	};
	
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnCount;
	}

	@Override
	public String getColumnName(int Index) {
	//System.out.println(this.columnNames.get(Index));
		return this.columnNames.get(Index);
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return rowCount;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return rows.get(rowIndex).get(columnIndex);
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex){
		return true;
	}
	
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex){
		try {
			rows.get(rowIndex).set(columnIndex, (String)aValue);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Vector<Vector<String>> getAllValues(){
		if (rows == null ){
			return new Vector<Vector<String>>();
		}
		return rows;
	}
	
	public void addRow(){
		Vector<String> tmp = new Vector<String>();
		for (int i=0 ; i < columnCount; i++ ){
			tmp.add("Test");
		}
		rows.add(tmp);
		rowCount = rowCount+1;
	}

	public void addRow(Vector<String> tmp){
		rows.add(tmp);
		rowCount = rowCount+1;
	}
	
	public void deleteRow(int index){
		if (index < rows.size()){
			rows.remove(index);
			rowCount = rowCount-1;			
		}
	}

}
